'use strict';

ionicTea.controller('notifyCtrl', function ($scope, $state, $stateParams, HttpService,UserService,$rootScope) {

	$scope.goToActivityNotify = function () {
		$state.go('notify-activity');
	};

	$scope.goToMyAward = function () {
		$state.go('notify-my-award');
	};

	$scope.goToNewNotify = function () {

		$rootScope.unreadNotifyMessage = $rootScope.unreadNotifyMessage - $scope.newdata.new_message_count;

		$scope.newdata.new_message_count = 0;

		$state.go('new-notify');
	};

	$scope.goto_myReply = function () {

		$rootScope.unreadNotifyMessage = $rootScope.unreadNotifyMessage - $scope.newdata.new_notification_count;

		$scope.newdata.new_notification_count = 0;

		$state.go('notify-my-reply');
	};

	$scope.goToAdPage = function (id) {
		$state.go('ad-detail', {id: id});
	};

	$scope.getAdList = function () {
		var callback = {
			success: function success(data) {
				$scope.adList = data;
			},
			failed: function failed(msg) {
				alert(msg);
			}
		};

		HttpService({ name: 'advert/list', params: {}, callback: callback });
	};

	$scope.initNotify = function() {
		$scope.get_newData_count();
		$scope.getAdList();
	}

	$scope.getAdDetail = function () {
		var callback = {
			success: function success(data) {
				$scope.ad = data;
			},
			failed: function failed(msg) {
				alert(msg);
			}
		};

		HttpService({ name: 'advert/show', params: {advert_id: $stateParams.id}, callback: callback });
	};
	
	//获取通知和回复数
	$scope.newdata = {};
	$scope.get_newData_count = function(){
		var user = UserService.getUserAcceptCache();
		if (!user) return;
		
		var callback = {
			success: function success(data) {
				console.log("查询新通知数量返回：");console.log(data);
				$scope.newdata.new_message_count = data.new_message_count;
				$scope.newdata.new_notification_count = data.new_notification_count;
				$rootScope.unreadNotifyMessage = data.new_message_count + data.new_notification_count;
			}
		};
		HttpService({ name: 'notification/newdata', params: {user_id: user.id}, callback: callback });
	}
	
	//获取新通知列表
	$scope.noti_list = [];
	$scope.get_new_notice = function(){
		var user = UserService.getUserAcceptCache();
		if (!user) return;
		
		var callback = {
			success: function success(data) {
				console.log("查询通知列表返回：");console.log(data);
				$scope.noti_list = data.result;
			}
		};
		HttpService({ name: 'notification/list', params: {user_id: user.id}, callback: callback });
	}
	
	//获取回复
	$scope.reply_list = [];
	$scope.get_reply = function(){
		var user = UserService.getUserAcceptCache();
		if (!user) return;
		
		var callback = {
			success: function success(data) {
				console.log("我的回复列表返回：");console.log(data);
				$scope.reply_list = data.result;
			}
		};
		HttpService({ name: 'notification/message', params: {user_id: user.id}, callback: callback });
	}

	$scope.back = function() {
		$rootScope.back();
	}
});
